"""Flask 扩展初始化"""
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_jwt_extended import JWTManager
from flask_cors import CORS
from flask_smorest import Api
from flask_babel import Babel

# 初始化扩展实例
db = SQLAlchemy()
migrate = Migrate()
jwt = JWTManager()
cors = CORS()
api = Api()
babel = Babel()


def init_extensions(app):
    """初始化所有扩展"""
    
    # 数据库扩展
    db.init_app(app)
    migrate.init_app(app, db)
    
    # JWT扩展
    jwt.init_app(app)
    
    # 跨域扩展
    cors.init_app(app, resources={
        r"/api/*": {
            "origins": "*",
            "methods": ["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"],
            "allow_headers": ["Content-Type", "Authorization"]
        }
    })
    
    # API文档扩展
    api.init_app(app)
    
    # 国际化扩展
    babel.init_app(app) 